package com.liyueheng.app.dataWarehouseDetail.portrait

import com.liyueheng.util.{ConfigLoader, SparkConf}
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions._

object GenderPortrait {
  def analyzeGender(): Unit = {
    println("------------------ 分析性别分布 -----------------")
    val appName = ConfigLoader.getString("app.name")
    val databaseDwd = ConfigLoader.getString("databases.dwd")
    val databaseDws = ConfigLoader.getString("databases.dws")
    val spark: SparkSession = SparkConf.createSparkSession(appName)

    try {
      val userDF = spark.table(s"$databaseDwd.user")

      val resultDF = userDF
        .withColumn("name",
          when(lower(col("gender")) === "male", "男")
            .when(lower(col("gender")) === "female", "女")
            .otherwise("未知")
        )
        .groupBy("name")
        .agg(count("*").alias("value"))

      resultDF.write.mode("overwrite").format("hive").saveAsTable(s"$databaseDws.portrait_gender")
    } finally {
      SparkConf.stopSparkSession(spark)
    }
  }
}
